package com.fzdy.dataplatform.service.impl;

import com.fzdy.dataplatform.core.dao.Dao;
import com.fzdy.dataplatform.core.service.impl.BaseServiceImpl;
import com.fzdy.dataplatform.dao.DeviceDao;
import com.fzdy.dataplatform.dao.DeviceOwnerDao;
import com.fzdy.dataplatform.entity.Device;
import com.fzdy.dataplatform.entity.DeviceOwner;
import com.fzdy.dataplatform.service.DeviceService;
import com.fzdy.dataplatform.webutil.CoapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

@Service
@Transactional
public class DeviceServiceImpl extends BaseServiceImpl<Device> implements DeviceService {

    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private DeviceOwnerDao deviceOwnerDao;
    @Autowired
    private CoapUtil coapUtil;

    @Override
    protected Dao getDao() {
        return deviceDao;
    }

    /**
     * 伪删除
     */

    @Override
    public void batchDelete(Serializable[] ids, Integer uid) {
        for (Serializable id : ids) {
            Device device = deviceDao.get(id);
            if (device != null) {
                if (device.getDeviceOwner().getUserid().equals(uid)) {
                    if (device.getBusiness() != null) {
                        if (device.getBusiness().getProduct() != null) {
                            if (device.getBusiness().getProduct().getCommunicationProtocol().getProtocol().contains("coap")) {
                                coapUtil.delDevice(device.getCoapdeviceid());
                            }
                        }
                    }
                    DeviceOwner deviceOwner = new DeviceOwner();
                    deviceOwner.setSn(device.getSn());
                    List<DeviceOwner> li = deviceOwnerDao.findByExampleEq(deviceOwner);
                    if (li.size() != 0) {
                        deviceOwnerDao.deleteT(li.get(0).getId());
                        device.setStatus(0);
                        deviceDao.update(device);
                    }
                }
            }
        }
    }

    @Override
    public List<Device> getListBySnList(List<String> listSn) {
        return deviceDao.getListBySnList(listSn);
    }

    @Override
    public void batchDelete(Serializable[] ids) {
        for (Serializable id : ids) {
            Device device = deviceDao.get(id);
            if (device != null) {
                if (device.getBusiness() != null) {
                    if (device.getBusiness().getProduct() != null) {
                        if (device.getBusiness().getProduct().getCommunicationProtocol().getProtocol().contains("coap")) {
                            coapUtil.delDevice(device.getCoapdeviceid());
                        }
                    }
                }
                DeviceOwner deviceOwner = new DeviceOwner();
                deviceOwner.setSn(device.getSn());
                List<DeviceOwner> li = deviceOwnerDao.findByExampleEq(deviceOwner);
                if (li.size() != 0) {
                    deviceOwnerDao.deleteT(li.get(0).getId());
                    deviceDao.deleteT(device.getSn());
                }
            }
        }
    }
}
